// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
const i18n= require('i18n');

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        canvas:cc.Node,

        touchLocationdisplay:{
            default:null,
            type:cc.Node
        },

        follower:{
            default:null,
            type:cc.Node
        },

        followSpeed:200

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var self=this;
        self.moveToPos=cc.p(0,0);

        self.isMoving=false;
        //触控开始
        self.canvas.on(cc.Node.EventType.TOUCH_START,function(event){

            var touches=event.getTouches();

            var  touchLoc=touches[0].getLocation();

            self.isMoving=true;

            self.moveToPos=self.follower.parent.convertToNodeSpaceAR(touchLoc);

            self.touchLocationdisplay.textKey=i18n.t("cases/03_gameplay/01_player_control/On/OnTouchCtrl.js.1")+Math.floor(touchLoc.x) +','+Math.floor(touchLoc.y)+')';

        },self.node);
        //触控进行中
        self.canvas.on(cc.Node.EventType.TOUCH_MOVE,function(event)
    {
        var touches=event.getTouches();

        var  touchLoc=touches[0].getLocation();

        self.moveToPos=self.follower.parent.convertToNodeSpaceAR(touchLoc);

        self.touchLocationdisplay.textKey=i18n.t("cases/03_gameplay/01_player_control/On/OnTouchCtrl.js.1")+Math.floor(touchLoc.x) +','+Math.floor(touchLoc.y)+')';
    },self.node);

        self.canvas.on(cc.Node.EventType.TOUCH_END,function(event)
    {
        self.isMoving=fasle;// when touch ended, stop moving

    },self.node);
    },

   
    update (dt) {
        if(!this.isMoving){return;}

        var oldPos=this.follower.position;

        var direction=cc.pNormalize(cc.pSub(this.moveToPos,oldPos));

        var newPos=cc.pAdd(oldPos,cc.pMult(direction,this.followSpeed*dt));
        
        this.follower.setPosition(newPos);
    },
});
